package vno.fux.init
{
	import fl.controls.ProgressBar;
	import fl.controls.ProgressBarMode;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import vno.fux.BaseDisplayObject;
	import vno.fux.CompassDirection;
	import vno.fux.GlobalEventScheduler;
	import vno.fux.GlobalVar;
	import vno.fux.utils.Mission;
	import vno.fux.utils.MissionManager;
	import vno.sci.event.MonologEvent;
	import vno.sci.window.GlowMessageBox;

	public class LoadingInterface extends Sprite
	{
		private var _bar:BaseDisplayObject;
		private var _initTumb:BaseDisplayObject;
		private var _userChoose:int;
		private var _chooseSex:GlowMessageBox;
		private var _boyLoad:BaseDisplayObject;
		private var _girlLoad:BaseDisplayObject;
		private var _nowLoad:BaseDisplayObject;
		public function LoadingInterface(settings_:XML)
		{
			var mm:MissionManager = new MissionManager;
			_bar = new BaseDisplayObject(new ProgressBar, CompassDirection.CENTER);
			_initTumb = new BaseDisplayObject(new Loader);
			_boyLoad = new BaseDisplayObject(new Loader);
			_girlLoad = new BaseDisplayObject(new Loader);
				
			(_bar.content as ProgressBar).mode = ProgressBarMode.MANUAL;
			(_bar.content as ProgressBar).minimum = 0;
			_bar.x = GlobalVar.stage.stageWidth/2;
			_bar.y = GlobalVar.stage.stageHeight/2 + GlobalVar.stage.stageHeight*0.25;
			_bar.width = GlobalVar.stage.stageWidth/2;
			_bar.height = 16;
			
			
			//(_initTumb.content as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			mm.addMission(new Mission((_initTumb.content as Loader).load, 
				new URLRequest(GlobalVar.webUrl + settings_.mapThumb.@url),(_initTumb.content as Loader).contentLoaderInfo,"",
				Event.COMPLETE));
			mm.addMission(new Mission((_boyLoad.content as Loader).load, 
				new URLRequest(GlobalVar.webUrl + settings_.boyLoad.@url),(_boyLoad.content as Loader).contentLoaderInfo,"",
				Event.COMPLETE));
			mm.addMission(new Mission((_girlLoad.content as Loader).load, 
				new URLRequest(GlobalVar.webUrl + settings_.girlLoad.@url),(_girlLoad.content as Loader).contentLoaderInfo,"",
				Event.COMPLETE));
			mm.addEventListener(Event.COMPLETE, completeHandler);
			mm.run();
			_initTumb.x = GlobalVar.stage.stageWidth/2;
			_initTumb.y = GlobalVar.stage.stageHeight/2;
			
			_chooseSex = new GlowMessageBox;

			addChild(_initTumb);
			addChild(_bar);

			this.addEventListener(Event.ADDED, 	addedHandler);
			this.addEventListener(Event.REMOVED, removeHandler);
		}

		public function get userChoose():int
		{
			return _userChoose;
		}

		public function userInit():void
		{
			_chooseSex.x = GlobalVar.stage.stageWidth/2 - _chooseSex.width/2 + 25;
			_chooseSex.y = GlobalVar.stage.stageHeight/2 - _chooseSex.height/2;
			_chooseSex.addEventListener(MonologEvent.CLICK, chooseHandler);
			addChild(_chooseSex);
		}
		
		protected function chooseHandler(event:MonologEvent):void
		{
			_userChoose = event.id;
			_chooseSex.removeEventListener(MonologEvent.CLICK, chooseHandler);
			_chooseSex.visible = false;
			if(_userChoose == 0)
			{
				addChild(_boyLoad);
				_nowLoad = _boyLoad;
			}
			else
			{
				addChild(_girlLoad);
				_nowLoad = _girlLoad;
			}

			GlobalEventScheduler.getInstance().addEnterFrameListener(loadFlash);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private var counter:Number = -0.02;
		private function loadFlash(event:Event):void
		{
			_nowLoad.alpha += counter;
			if(_nowLoad.alpha>=1||_nowLoad.alpha<=0.25) counter = -counter;
		}
		protected function addedHandler(event:Event):void
		{
			this.removeEventListener(Event.ADDED, addedHandler);
			GlobalEventScheduler.getInstance().addResizeListener(resizeHandler);
		}
		
		protected function removeHandler(event:Event):void
		{
			this.removeEventListener(Event.REMOVED, removeHandler);
			this.removeChildren();//this is very important for the GC, while I don't know why.
			GlobalEventScheduler.getInstance().removeResizeListener(resizeHandler);
			GlobalEventScheduler.getInstance().removeEnterFrameListener(loadFlash);
		}
		
		private function completeHandler(event:Event):void
		{
			(event.target as MissionManager).removeEventListener(Event.COMPLETE, completeHandler);
			_initTumb.setRegPoint(CompassDirection.CENTER);//refresh regpoint
			_boyLoad.setRegPoint(CompassDirection.CENTER);
			_girlLoad.setRegPoint(CompassDirection.CENTER);
			
			_boyLoad.x = GlobalVar.stage.stageWidth/2;
			_boyLoad.y = _bar.y+_bar.height/2+_boyLoad.height/2+10;
			_girlLoad.x = GlobalVar.stage.stageWidth/2;
			_girlLoad.y = _bar.y+_bar.height/2+_girlLoad.height/2+10;

			dispatchEvent(event);
		}
		public function handleProgress(event:ProgressEvent):void
		{
			(_bar.content as ProgressBar).setProgress(event.bytesLoaded, event.bytesTotal);
		}
		private function resizeHandler(event:Event):void
		{
			_initTumb.x = GlobalVar.stage.stageWidth/2;
			_initTumb.y = GlobalVar.stage.stageHeight/2;
			_bar.x = GlobalVar.stage.stageWidth/2;
			_bar.y = GlobalVar.stage.stageHeight/2 + GlobalVar.stage.stageHeight*0.25;
			_bar.width = GlobalVar.stage.stageWidth/2;
			_boyLoad.x = GlobalVar.stage.stageWidth/2;
			_boyLoad.y = _bar.y+_bar.height/2+_boyLoad.height/2+10;
			_girlLoad.x = GlobalVar.stage.stageWidth/2;
			_girlLoad.y = _bar.y+_bar.height/2+_girlLoad.height/2+10;
			try
			{
				_chooseSex.x = GlobalVar.stage.stageWidth/2 - _chooseSex.width/2 + 25;
				_chooseSex.y = GlobalVar.stage.stageHeight/2- _chooseSex.height/2;
			}
			catch(e:Error)
			{
				trace(e);
			}
		}
	}
}